package news.web;

import com.github.pagehelper.PageInfo;
import news.model.Admin;
import news.model.CommonResult;
import news.model.NewsType;
import news.model.Role;
import news.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/admin/newsType")

public class NewsTypeController {
    @Autowired(required = false)
    NewsTypeService newsTypeService;

    //查询所有新闻类型元素并分页
    @RequestMapping("/newsTypes")
    public CommonResult newsTypes(@RequestBody NewsType newsType) {
        PageInfo<NewsType> newsTypes = newsTypeService.newsTypeList(newsType);
        newsTypes.getList();
        newsTypes.getTotal();
        CommonResult commonResult = new CommonResult(200, newsTypes, "查询成功");
        return commonResult;
    }

    //删除新闻类型
    @GetMapping(path = "/delete")
    public CommonResult delete(Integer id) {
        newsTypeService.delete(id);
        CommonResult commonResult = new CommonResult(200, "删除成功");
        return commonResult;
    }

    @PostMapping(path = "/save")
    public CommonResult save(@RequestBody NewsType newsType, @RequestHeader String adminToken) {
        //判断传到后端的值是否有id，有就是更新，没有就是新增
        if (newsType.getId() == null) {
            newsTypeService.save(newsType,adminToken);
        }
        else{
            newsTypeService.update(newsType,adminToken);
        }
        CommonResult commonResult = new CommonResult(200,"保存成功");
        return commonResult;
    }

    //根据id查找新闻类型
    @GetMapping(path = "/findTypeById")
    public CommonResult findTypeById(Integer id){
        NewsType newsType = newsTypeService.findTypeById(id);
        CommonResult commonResult = new CommonResult(200,newsType,"查询成功");
        return commonResult;
    }
}
